﻿<Window 
    x:Class="WpfUndoSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Monitored Undo Framework Sample" Height="350" Width="725"
    Name="LayoutRoot"
    Loaded="LayoutRoot_Loaded"
>
    <Window.CommandBindings>
        <CommandBinding Command="Undo" Executed="Undo_Executed" CanExecute="Undo_CanExecute" />
        <CommandBinding Command="Redo" Executed="Redo_Executed" CanExecute="Redo_CanExecute" />
    </Window.CommandBindings>

    <Grid Background="White" Margin="20">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width=".5*" />
            <ColumnDefinition Width=".5*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        
        <!--
            NOTE:
            Textboxes is bound "Two Way" so that it will pick up changes in the model.
        -->
        
        <Label Grid.Row="0" Grid.Column="0">First Name:</Label>
        <TextBox 
            Name="FirstNameTextbox"
            Grid.Row="0" Grid.Column="1"
            Text="{Binding ElementName=LayoutRoot, Path=FirstName, Mode=TwoWay}" 
            HorizontalAlignment="Stretch" Margin="10,2" 
            VerticalAlignment="Top" MinWidth="120"
            IsUndoEnabled="False" 
        />

        <Label Grid.Row="1" Grid.Column="0">Last Name:</Label>
        <TextBox 
            Grid.Row="1" Grid.Column="1"
            Text="{Binding ElementName=LayoutRoot, Path=LastName, Mode=TwoWay}" 
            HorizontalAlignment="Stretch" Margin="10,2" 
            VerticalAlignment="Top" MinWidth="120" 
            IsUndoEnabled="False" 
        />

        <Label Grid.Row="2" Grid.Column="0">Full Name:</Label>
        <Label 
            Grid.Row="2" Grid.Column="1" Margin="10,2" 
            Content="{Binding ElementName=LayoutRoot, Path=FullName, Mode=OneWay}" />

        <!--
            NOTE:
            Slider is bound "Two Way" so that it will pick up changes in the model.
            
            We capture the Mouse events so that we can "batch" the changes that happen
            between mouse down and mouse up. Otherwise, each incremental movement while
            dragging the mouse will result in a separate undo entry.
        -->
        <Label Grid.Row="3" Grid.Column="0">Age:</Label>
        <Slider 
            Grid.Row="3" Grid.Column="1" Margin="10,2"
            Minimum="0" Maximum="120" 
            TickPlacement="Both" TickFrequency="10"
            Value="{Binding ElementName=LayoutRoot, Path=Age, Mode=TwoWay}" 
            PreviewMouseLeftButtonDown="Slider_MouseDown" 
            LostMouseCapture="Slider_LostMouseCapture"
        />

        <TextBlock 
            Grid.Row="4" Grid.Column="1" Margin="20,2" 
        >
            <Run Text="{Binding ElementName=LayoutRoot, Path=Age, Mode=OneWay}" />
            <Run Text=" years old" />
        </TextBlock>
        
        <CheckBox
            Grid.Row="5" Grid.Column="1" Margin="20,2"
            Content="Batch changes"
            IsChecked="{Binding ElementName=LayoutRoot, Path=BatchAgeChanges, Mode=TwoWay}"
        />

        
        <UniformGrid Rows="1" Grid.Row="7" Grid.ColumnSpan="2" Margin="10">
            <Button Content="Undo (CTL+Z)" Height="23" HorizontalAlignment="Left" Margin="10,2" 
                Name="UndoButton" VerticalAlignment="Top" Width="100" Command="Undo" />

            <Button Content="Redo (CTL+Y)" Height="23" HorizontalAlignment="Left" Margin="10,2" 
                Name="RedoButton" VerticalAlignment="Top" Width="100" Command="Redo" />
        </UniformGrid>

        <Label Grid.Row="0" Grid.Column="2">Undo Stack:</Label>
        <ListBox 
            x:Name="UndoStackListBox"
            Grid.Row="1" Grid.RowSpan="10"
            Grid.Column="2"
            Margin="10"
            ItemsSource="{Binding ElementName=LayoutRoot, Path=UndoStack, Mode=OneWay}"
            IsEnabled="True"
            HorizontalAlignment="Stretch"
        >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Description, Mode=OneWay}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <Label Grid.Row="0" Grid.Column="3">Redo Stack:</Label>
        <ListBox 
            x:Name="RedoStackListBox"
            Grid.Row="1" Grid.RowSpan="10"
            Grid.Column="3"
            Margin="10"
            ItemsSource="{Binding ElementName=LayoutRoot, Path=RedoStack, Mode=OneWay}"
            IsEnabled="True"
            HorizontalAlignment="Stretch"
        >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Description, Mode=OneWay}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>

</Window>
